﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Phoenix.AppCode.Entities;
using System.Data.SqlClient;
using System.Data;

namespace Phoenix.AppCode.Data
{
    public class ContactDAO : BaseDAO
    {
        public void Create(Contact contact)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("sp_ContactCreate", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@FullName", contact.FullName));
                command.Parameters.Add(new SqlParameter("@Email", contact.Email));
                command.Parameters.Add(new SqlParameter("@Subject", contact.Subject));
                command.Parameters.Add(new SqlParameter("@Content", contact.Content));
                connection.Open();
                command.ExecuteNonQuery();
                //if (paramId.Value != DBNull.Value)
                //articlecategory.ID = (long)paramId.Value;
                //else
                //  throw new DataAccessException(String.Format(Resources.Strings.CreateEntityException, "Category"));
            }
        }

        #region Backend Method
        /// <summary>
        /// Check mail Contact
        /// </summary>
        /// <param name="category"></param>
        public void Edit(Contact contact)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spContactEdit", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_Contact", contact.ID_Contact));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.UpdateEntityException, "Category", category.ID));
            }
        }

        /// <summary>
        /// Delete Contact
        /// </summary>
        /// <param name="category"></param>
        public void Delete(Contact contact)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spContactDelete", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_Contact", contact.ID_Contact));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.DeleteEntityException, "Category", category.ID));
            }
        }

        /// <summary>
        /// Show Contact All
        /// </summary>
        /// <returns></returns>
        public DataTable ContactList()
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spContactList", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;
            }
        }

        /// <summary>
        /// Show data by ID
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public DataTable ContactList_GetByID(int idcontact)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spContactEdit_GetByID", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ID_Contact", SqlDbType.Int).Value = idcontact;
                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;
            }
        }

        /// <summary>
        /// Get ID to Edit or Delete
        /// </summary>
        /// <param name="idcontact"></param>
        /// <returns></returns>
        public Contact GetContactByID(int idcontact)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spContactEdit_GetByID", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_Contact", idcontact));

                connection.Open();
                Contact contact = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                        contact = CreateContactFromReader(reader);
                    //else
                    //   throw new DataAccessException(String.Format(Resources.Strings.GetEntityException, "Category", ID));
                }

                return contact;
            }
        }

        private Contact CreateContactFromReader(IDataReader reader)
        {
            Contact contact = new Contact();
            contact.ID_Contact = (int)reader["ID_Contact"];
            contact.FullName = (string)reader["FullName"];
            contact.Email = (string)reader["Email"];
            contact.Subject = (string)reader["Subject"];
            contact.Content = (string)reader["Content"];
            contact.State = (Boolean)reader["State"];
            contact.PostDate = (DateTime)reader["PostDate"];
            return contact;
        }
        #endregion
    }
}